package tables

import (
	"peilian-api/app/global/variable"
	"time"
)

const (
	InitStatus = -1 // 初始得分
	NoStatus   = 0  // 面试场次建立，未完成报告
	SucStatus  = 1  // 面试报告生成，正常面试完成
	ProStatus  = 2  // 面试报告生成中
	IoStatus   = 3  // 正常面试视频流接收完成

	TranslateStatus      = 0 // 待翻译
	TranslateDoneStatus  = 1 // 翻译完成
	TranslateDoingStatus = 2 // 翻译中
)

type ScoreFeedBack struct {
	Model
	SessionID uint `json:"session_id"`
	Session   *Session
	ScoreType string `gorm:"type:varchar(20);comment:total-报告总分/profession-专业能力评分/general-通用岗位评分/expressive-面试状态表达能力评分/theme-面试状态重点把控力评分/logic-面试状态逻辑性评分" json:"score_type"`
	Score     int    `gorm:"type:decimal(5,2);default:0;comment:面试总分" json:"score"`
	FeedBack  string `gorm:"comment:评分反馈,nice/bad" json:"feed_back"`
}

func (*ScoreFeedBack) TableName() string {
	return "session_score"
}

type Session struct { //场次表
	Model
	UserID     uint   `json:"user_id" gorm:"type:uint"`
	Sno        string `json:"sno" gorm:"type:varchar(16);not null;default:\"\";comment:学号"`
	User       *User
	CareerID   uint
	Career     *Career
	UserPhoto  string     `json:"user_photo" gorm:"type:varchar(255);comment:用户照片"`
	Status     uint       `gorm:"type:uint;default:0;comment:0:是未生成 1:报告生成完成 2:报告生成中 3:视频面试完成"`
	Url        string     `gorm:"type:varchar(64);comment:报告的地址"`
	TotalScore float64    `gorm:"type:float;comment:报告总分;index"`
	FinishAt   time.Time  `gorm:"comment:面试结束时间"`
	TrainAt    time.Time  `gorm:"comment:面试开始时间"`
	RealQues   []RealQues `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
	Type       string     `gorm:"type:varchar(50);comment:用户类型,同user表中type"`
	Evaluation string     `gorm:"comment:v1.0算法评测结果"`

	//以下信息根据需要存储
	School     string `json:"school" gorm:"type:varchar(100);comment:学校"`
	Academy    string `json:"academy" gorm:"type:varchar(255);comment:学院"`
	Profession string `json:"profession" gorm:"type:varchar(255);comment:专业"`
	Grade      string `gorm:"type:varchar(50);comment:年级"`
	UserName   string `gorm:"type:varchar(50)"`
	UserPhone  string `json:"phone" gorm:"type:varchar(20);comment:手机"`
	CareerName string `gorm:"type:varchar(50);comment:题库名称"`
	Education  string `gorm:"type:varchar(10);default:\"\";comment:学历"`
	Duration   uint   `gorm:"type:uint;default:0;comment:训练时长"`

	IsShared bool `gorm:"comment:是否分享过了"`
	IsSync   bool `gorm:"default:true;comment:是否开启训练记录同步"`
	IsRead   bool `gorm:"default:false;comment:是否已经打开该报告"`
}

func (s *Session) TableName() string {
	return "session"
}

func (s *Session) FindReportsByUID(uid int) ([]Session, error) {
	var reports []Session
	statusLi := []int{SucStatus, ProStatus}
	err := variable.DB.MySQL.Where("status in (?) and finish_at<>?", statusLi, "0000-00-00 00:00:00").
		Preload("User").Preload("Career").Order("created_at desc").Find(&reports, "user_id = ?", uid).Error
	if err != nil {
		return nil, err
	}
	return reports, nil
}

// 根据条件查询报告map
func (s *Session) FindReportsByWhereMap(where map[string]interface{}) ([]Session, error) {
	var reports []Session
	err := variable.DB.MySQL.Where(where).Find(&reports).Error
	if err != nil {
		return nil, err
	}
	return reports, nil
}

func (s *Session) FindReportsByWhereStr(where string) ([]Session, error) {
	var reports []Session
	err := variable.DB.MySQL.Where(where).Preload("User").Preload("Career").Order("created_at desc").Find(&reports).Error
	if err != nil {
		return nil, err
	}
	return reports, nil
}

// 查询该用户的报告数据
func (s *Session) FindSessionCountByUid(uid uint) (int64, error) {
	var count int64 = 0
	err := variable.DB.MySQL.Model(&s).Where("user_id = ? and status = 1", uid).Count(&count).Error
	if err != nil {
		return count, err
	}
	return count, nil
}
